package com.qikux.controller;

import cn.hutool.captcha.AbstractCaptcha;
import com.qikux.constraint.RssConst;
import com.qikux.dto.ResourceDTO;
import com.qikux.entity.Resource;
import com.qikux.entity.ResourceType;
import com.qikux.entity.User;
import com.qikux.service.ResourceService;
import com.qikux.utils.UploadUtils;
import com.qikux.vo.PageVo;
import com.qikux.vo.R;
import org.haredot.annotation.*;
import org.haredot.web.ModelAndView;
import org.haredot.web.RequestMethod;

import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import java.util.List;

@Controller
@RequestMapping("/res")
public class ResourceController {

    @org.haredot.annotation.Resource
    private ResourceService resourceService ;

    @RequestMapping(value = "/upload", method = RequestMethod.GET)
    public ModelAndView upload() {
        // 查询所有的资源分类
        return new ModelAndView("upload");
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public @ResponseBody R<String> upload(@RequestPart("resource")Part part) throws Exception {
        // 上传到服务器
        String path = UploadUtils.upload(part, RssConst.RESOURCE_DIR) ;

        return R.<String>ok().data(path);
    }

    @RequestMapping(value = "/types", method = RequestMethod.GET)
    public @ResponseBody R<List<ResourceType>> types() throws Exception {
        // 调用业务逻辑层 查询数据
        return resourceService.queryTypes();
    }


    @RequestMapping(value = "/checkCode", method = RequestMethod.GET)
    public @ResponseBody R<?> checkCode(@RequestParam("code")String code, HttpSession session) throws Exception {

        // 从 session 中 获取 服务器 存储的验证码
        AbstractCaptcha captcha = (AbstractCaptcha)session.getAttribute(RssConst.CAPTCHA_CODE);

        if (captcha == null) return R.fail("验证码不正确");

        boolean ret = captcha.verify(code) ;

        if (ret) return R.ok() ;

        return R.fail("验证码不正确");
    }


    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody R<?> addResource(
            @RequestParam("resourceName") String resourceName,
            @RequestParam("description") String description,
            @RequestParam("resourceUrl") String resourceUrl,
            @RequestParam("keywords") String keywords,
            @RequestParam("typeId") Long typeId,
            @RequestParam("score") Integer score,
            @RequestParam("size") Long size,
            @RequestParam("ext") String ext,
            @RequestParam("code")String code, HttpSession session) throws Exception {

        // 从 session 中 获取 服务器 存储的验证码
        AbstractCaptcha captcha = (AbstractCaptcha)session.getAttribute(RssConst.CAPTCHA_CODE);

        // 将 验证码的对象 从 session 中移除
        session.removeAttribute(RssConst.CAPTCHA_CODE);

        // 从 session 中 获取 登录的用户
        User user = (User)session.getAttribute(RssConst.CURRENT_USER);

        if (captcha == null) return R.fail("验证码不正确");

        // 如果 校验成功
        if (captcha.verify(code)) {
            // 组装数据
            Resource resource = Resource.builder().resourceName(resourceName)
                    .resourceUrl(resourceUrl)
                    .description(description)
                    .keywords(keywords)
                    .typeId(typeId)
                    .score(score)
                    .size(size)
                    .ext(ext)
                    .userId(user.getId())
                    .build();
            // 调用业务层处理业务
            return resourceService.saveResource(resource);
        }

        return R.fail("验证码不正确");
    }


    @RequestMapping(value = "/catalog", method = RequestMethod.GET)
    public ModelAndView catalog() {
        return new ModelAndView("catalog");
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public @ResponseBody R<PageVo<ResourceDTO>> queryResource(@RequestParam("type")Long typeId,
                                                            @RequestParam(value = "page", required = false, defaultValue = "1") Long page,
                                                            @RequestParam(value = "rows", required = false, defaultValue = "12") Long rows) {
        // 封装一个专门处理分页的类 limit offset,  rows
        //  offset = (page - 1) * rows ;
        PageVo<ResourceDTO> pageVo = new PageVo<ResourceDTO>(page, rows);

        // 根据资源类型ID 查询该资源下的所有信息
        return resourceService.queryResourceByType(typeId, pageVo);
    }

}
